<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form">
    <div class="mainBox  layui-card-body" style="padding: 0 0;">
        <div class="main-container">
            <div class="layui-tab" lay-filter="TabBrief">
                <ul class="layui-tab-title">
                    <li class="layui-this">One Flex</li>
                    <li>Open Flex</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div id="one"></div>
                    </div>
                    <div class="layui-tab-item">
                        <div id="open"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['form', 'jquery', 'element', 'transfer'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let element = layui.element;
        let transfer = layui.transfer;

        let cate_id = {$cate_id};
        let flex_id = {$flex_id};

        $.ajax({
            url: "/Flex/getXuankeInfo",
            data:{cate_id:cate_id, flex_id:flex_id},
            dataType: 'json',
            type: 'post',
            success: function(res) {

                //显示搜索框
                transfer.render({
                    elem: '#one'
                    ,data: res.data.oneflex
                    ,width: 350
                    ,height: 600
                    ,text: {
                        none: '无数据' //没有数据时的文案
                        ,searchNone: '无匹配数据' //搜索无匹配数据时的文案
                    }
                    ,title: ['One Flex', '开放的One Flex']
                    ,showSearch: true
                    ,value: res.data.oneflex_str
                    ,id: 'one'
                })

                transfer.render({
                    elem: '#open'
                    ,data: res.data.openflex
                    ,width: 350
                    ,height: 600
                    ,text: {
                        none: '无数据' //没有数据时的文案
                        ,searchNone: '无匹配数据' //搜索无匹配数据时的文案
                    }
                    ,title: ['Open Flex', '开放的Open Flex']
                    ,showSearch: true
                    ,value: res.data.openflex_str
                    ,id: 'open'
                })
            }
        })


        let tabid = 0;
        element.on('tab(TabBrief)', function(data){
            tabid = data.index;
        });


        form.on('submit(user-save)', function(data) {

            if (tabid == 0) {

                var datas = transfer.getData('one');
                var type = '1flex';

            } else if(tabid == 1) {

                var datas = transfer.getData('open');
                var type = 'openflex';

            }

            if (datas == '') {
                var courseid = [];
            } else {
                var courseid = [];
                for ( var i = 0; i < datas.length; i++){
                    courseid.push(datas[i].value);
                }
            }

            layer.confirm('确定保存吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Flex/saveCourse",
                    data:{cate_id:'{$cate_id}', type:type, courseid:courseid, flex_id:'{$flex_id}'},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>